package Leo.serviceProviders.controller;

import Leo.serviceProviders.config.HealthIndicatorImpl;
import Leo.serviceProviders.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
public class FaqController {
    @Autowired
    HealthIndicatorImpl healthIndicator;

    int a = 0;

    @RequestMapping("/query/{id}")
    public String query(@PathVariable("id") String id, HttpServletRequest request){
        System.out.println("服务器负载均衡的证明一：i got id:" + id);
        String result = "#############\nquery question :" + id + "\n" + request.getRequestURL().toString()+"\n#############";

        return result;
    }

    @RequestMapping("/test")
    public String query(){
        return "success!!";
    }

    /**
     * 设置健康状态
     * @param health
     * @return
     */
    @RequestMapping("/setHealth")
    public String setHealth(String health){
        if("ture".equals(health)){
            healthIndicator.setHealthFlag(true);
        }else{
            healthIndicator.setHealthFlag(false);
        }

        return "setHealth success!!";
    }

    /**
     * 用于测试返回entity
     * @param userId
     * @return
     */
    @RequestMapping("/getUser/{userid}")
    public User getUser(@PathVariable("userid") String userId){
        User user = new User();
        user.setUserName(randomUserName());
        Random random = new Random();
        user.setUserId(userId);
        return user;
    }

    @RequestMapping("/zj/{userid}")
    public User zj(@PathVariable("userid") String userId) throws InterruptedException {
        if("zj".equals(userId)){
            a = 20;
            Thread.sleep(600000);
        }
        User user = new User();
        user.setUserId(a + "");
        user.setUserName("霸刀宋壹");

        return user;
    }

    /**
     * 用于测试传入entity
     * @param user
     * @return
     */
    @RequestMapping(value = "/sendUser",method = RequestMethod.POST)
    public String sendUser(@RequestBody User user) {
        return "user name is :" + user.getUserName();
    }

    @RequestMapping(value = "/xmlUser", //rest路径
            method = RequestMethod.POST //指定访问方法
            ,consumes = MediaType.APPLICATION_XML_VALUE,  //指定处理请求的提交内容类型（Content-Type）
            produces = MediaType.APPLICATION_XML_VALUE  //指定返回值类型，不但可以设置返回值类型还可以设定返回值的字符编码
//                    produces = "MediaType.APPLICATION_XML_VALUE;charset=utf-8"
    )
    public String xmlUser(@RequestBody User user) {
        System.out.println("xml receive user: " + user.getUserName());
        String result = "<result><message>received success! " + user.getUserName() + "</message></result>";
        return result;
    }

    @RequestMapping(value = "/stringUser",method = RequestMethod.POST
//            ,consumes = MediaType.APPLICATION_XML_VALUE,  //指定处理请求的提交内容类型（Content-Type）
//            produces = MediaType.APPLICATION_XML_VALUE  //指定返回值类型，不但可以设置返回值类型还可以设定返回值的字符编码)
    )
    public String stringUser(@RequestParam("content") String content) {
        System.out.println(content);
        String result = "<result><message>received success! " +content + "</message></result>";
        return result;
    }

    /**
     * 批量获取user
     * @return
     */
    @RequestMapping(value = "/getUsers",method = RequestMethod.POST) //指定访问方法
//            consumes = MediaType.TEXT_HTML_VALUE ) //指定处理请求的提交内容类型（Content-Type）)
    public Map<String,User> getUsers(@RequestParam("userId")String userId){
        HashMap<String, User> userHashMap = new HashMap<String, User>();
        String[] split = userId.split(",");
        for (String s : split) {
            if(s != null && !s.equals(""))
                userHashMap.put(s,getUser(s));
        }
        return userHashMap;
    }

    private String randomUserName(){
        ArrayList<String> nameList = new ArrayList<String>();
        nameList.add("苏氏阿七");
        nameList.add("白真君");
        nameList.add("等待与诺言仙子");
        nameList.add("东方六仙子");
        nameList.add("荔枝仙子");
        nameList.add("道子");
        nameList.add("赤霄子");
        nameList.add("铜卦天仙");
        nameList.add("北河散人");
        nameList.add("苏氏阿十六");
        nameList.add("雨柔子");
        nameList.add("黄山真君");
        nameList.add("七修玄圣");
        nameList.add("葱娘");
        Random random = new Random();
        int i = random.nextInt(nameList.size());
        return nameList.get(i);
    }
}
